package com.ocr;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.ocr.*;
import com.adapter.LanguageAdapter;
import com.adapter.StringAdapter;
import com.entity.HistoryItem;
import com.entity.Language;
import com.entity.LanguageContext;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class SelectTimeDelActivity extends Activity {
	private ListView listViewDelTime;
	private List<String> listItem = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.popup_del_history);
		listViewDelTime = (ListView) findViewById(R.id.list_select_time);
		listItem.add(getResources().getString(R.string.del_1_hour));
		listItem.add(getResources().getString(R.string.del_1_day));
		listItem.add(getResources().getString(R.string.del_1_month));
		listItem.add(getResources().getString(R.string.del_all));
		StringAdapter adapter = new StringAdapter(getApplicationContext(),
				R.layout.item_language, listItem);
		listViewDelTime.setAdapter(adapter);
		listViewDelTime.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				// TODO Auto-generated method stub
				Date  date=new Date();
				switch (pos) {
				case 0: {
					long MILLISECONDS_PER_DAY = 1000L * 60 * 60;//1 hour
					long time = date.getTime();
				    time -= MILLISECONDS_PER_DAY;
				    date.setTime(time);
					MainActivity.HISTORY_DAO.deleteDuringTime(date);
				}
					break;
				case 1: {
					long MILLISECONDS_PER_DAY = 1000L * 60 * 60*24;//1 day
					long time = date.getTime();
				    time -= MILLISECONDS_PER_DAY;
				    date.setTime(time);
					MainActivity.HISTORY_DAO.deleteDuringTime(date);
				}
					break;
				case 2: {
					long MILLISECONDS_PER_DAY = 1000L * 60 * 60*24*30;//1 month
					long time = date.getTime();
				    time -= MILLISECONDS_PER_DAY;
				    date.setTime(time);
					MainActivity.HISTORY_DAO.deleteDuringTime(date);
				}
					break;
				case 3: {
					MainActivity.HISTORY_DAO.deleteAll();
					HistoryActivity.historyAdapter.clear();
					HistoryActivity.historyAdapter.notifyDataSetChanged();
				}
					break;
				default:
					break;
				}
				HistoryActivity.listHistory.clear();
				for (HistoryItem historyItem : MainActivity.HISTORY_DAO
						.loadAll())
					HistoryActivity.listHistory.add(historyItem);
				Collections.sort(HistoryActivity.listHistory);
				HistoryActivity.historyAdapter.notifyDataSetChanged();
				
				if(HistoryActivity.listHistory.size()==0||HistoryActivity.listHistory==null)
				{
					HistoryActivity.layoutNoHistory.setVisibility(View.VISIBLE);
				}
				SelectTimeDelActivity.this.finish();
			}
		});
	}

}
